route.ts 816 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { NextRequest, NextResponse } from 'next/server';
  2. const API_URL = process.env.API_URL;
  3. export async function GET(_: NextRequest, { params }: { params: Promise<{ path: string[] }> })
  4. {
  5. const { path } = await params;
  6. const filePath = `/uploads/${path.join('/')}`;
  7. try {
  8. const res = await fetch(`${API_URL}${filePath}`, {
  9. cache: 'no-store'
  10. });
  11. if (!res.ok) {
  12. return new NextResponse(null, {
  13. status: res.status
  14. });
  15. }
  16. const contentType = res.headers.get('content-type') || 'application/octet-stream';
  17. const buffer = await res.arrayBuffer();
  18. return new NextResponse(buffer, {
  19. status: 200,
  20. headers: {
  21. 'Content-Type': contentType,
  22. 'Cache-Control': 'public, max-age=31536000, immutable'
  23. }
  24. });
  25. } catch {
  26. return new NextResponse(null, {
  27. status: 502
  28. });
  29. }
  30. }